var city = require('../../../utils/city.js')
var lineHeight = 0;
var endWords = "";
var isNum;
//var viewList=[];
Page({
  data: {
    "hidden": true,
    cityName: "", //获取选中的城市名
    viewList: [],
    selListHeight: 0,
    title: null,
    deviceInfo: {
      usedNum: 0,
      totalNum: 0,
      detailInfo: []
    }
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this
    that.setData({
    //  deviceInfo: JSON.parse(options.list),
    //  title: options.text
    })
    var list = []
    var string = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    var length = 10
    for (let i = 0; i < length; i++) {
      var listItem = []
      for (let j = 0; j < 10; j++) {
        listItem[j] = {
          'num': j + i * 10 + 1
        }
      }
      /**js 对象属性名为变量复制方法 */
      var obj = {}
      obj[string[i]] = listItem
      list[i] = obj
    }
    
    that.setData({
      viewList: list
    })
  //  that.data.viewList = list
    console.log(that.data.viewList)
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    var that = this;
    var viewListChild = that.data.viewList;
    var cityChild = city.City[0];
    
    wx.getSystemInfo({
      success: function (res) {
        lineHeight = (res.windowHeight - 100) / 22;
      //  console.log(res.windowHeight - 100)
        that.setData({
          viewList: viewListChild,
          city: cityChild,
          winHeight: res.windowHeight - 40,
          lineHeight: lineHeight
        })
      }
    })
    //创建节点选择器
    var query = wx.createSelectorQuery();
    query.select('#selList').boundingClientRect()
    query.exec((res) => {
      that.data.selListHeight = res[0].height; // 获取selList高度
    })
  },
  onShow: function () {
    // 生命周期函数--监听页面显示

  },
  onHide: function () {
    // 生命周期函数--监听页面隐藏

  },
  onUnload: function () {
    // 生命周期函数--监听页面卸载

  },
  //触发全部开始选择
  chStart: function () {
    this.setData({
      trans: ".3",
      hidden: false
    })
  },
  //触发结束选择
  chEnd: function () {
    this.setData({
      trans: "0",
      hidden: true,
      scrollTopId: this.endWords
    })
    console.log('chEnd endWords', this.data.scrollTopId)
    console.log('chEnd endWords', this.endWords)
  },
  //获取文字信息
  getWords: function (e) {
    var id = e.target.id;
    this.endWords = id;
    isNum = id;
    this.setData({
      showwords: this.endWords
    })
  },
  //设置文字信息
  setWords: function (e) {
    var id = e.target.id;
    this.setData({
      scrollTopId: id
    })
    console.log('setWords scrollTopId', this.data.scrollTopId)
  },

  // 滑动选择城市
  chMove: function (e) {
    var y = e.touches[0].clientY;
    var offsettop = e.currentTarget.offsetTop;
    var height = 0;
    var that = this;
    var cityarr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
    // 获取y轴最大值
    wx.getSystemInfo({
      success: function (res) {
      //  height = res.windowHeight - 10;
      height = that.data.selListHeight + 40;
      }
    });

    //判断选择区域,只有在选择区才会生效
    if (y > offsettop && y < height) {
      // console.log((y-offsettop)/lineHeight)
      var num = parseInt((y - offsettop) / lineHeight);
      endWords = cityarr[num];
      // 这里 把endWords 绑定到this 上，是为了手指离开事件获取值
      that.endWords = endWords;
    };


    //去除重复，为了防止每次移动都赋值 ,这里限制值有变化后才会有赋值操作，
    //DOTO 这里暂时还有问题，还是比较卡，待优化
    if (isNum != num) {
      // console.log(isNum);
      isNum = num;
      console.log(num)
      that.setData({
        showwords: that.endWords,
        scrollTopId: that.endWords
      })
      console.log('chMove scrollTopId', this.data.scrollTopId)
    }
  },
  //选择城市，并让选中的值显示在文本框里
  bindCity: function (e) {
    /*
    console.log(e);
    var cityName = e.currentTarget.dataset.city;
    this.setData({
      cityName: cityName
    })
    */
    
  }

})